package cn.zirao

import android.content.Context
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import zirao.base.activity.BaseActivity
import zirao.base.recycler.BaseRecyclerAdapter
import zirao.base.recycler.BaseRecyclerVH
import kotlinx.android.synthetic.main.activity_recycler.*
import java.util.*

class RecyclerActivity : BaseActivity(R.layout.activity_recycler, 0) {

    private var mAdapter: MyAdapter? = null

    override fun onBackPressed() {
        closeActivity(this)
    }

    override fun initView() {
        mAdapter = MyAdapter(this)
    }

    override fun initData() {
        val list = LinkedList<String>()
        list.add("1")
        list.add("2")
        list.add("3")
        list.add("4")
        list.add("5")

        mAdapter!!.setList(list)
//        addLastData(list)

        recycler_list_rv!!.layoutManager = LinearLayoutManager(this)
        recycler_list_rv!!.adapter = mAdapter

        recycler_list_rv!!.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
    }

    override fun initListener() {
    }

    override fun widgetClick(v: View?) {
    }

    private fun addLastData(list: List<String>){
        for (item in list) mAdapter!!.addLast(item)
    }

    internal inner class MyViewHolder(itemView: View) : BaseRecyclerVH(itemView) {
        var name: TextView? = null
        var delete: Button? = null
    }

    internal inner class MyAdapter(private val context: Context) : BaseRecyclerAdapter() {
        override fun getViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
            val view : View? = LayoutInflater.from(context).inflate(R.layout.recycler_rv_item, parent, false)
            val holder : MyViewHolder? = MyViewHolder(view!!)
            holder!!.name = view.findViewById(R.id.recycler_item_name)
            holder!!.delete = view.findViewById(R.id.recycler_item_delete)

            return holder
        }

        override fun setVHData(holder: RecyclerView.ViewHolder?, data: Any?, position: Int) {
            val local = data as String
            val hold = holder as MyViewHolder

            hold.name!!.text = local
            hold.delete!!.setOnClickListener {
                removeObject(hold.adapterPosition)
            }
        }
    }
}
